package com.frskycockpit.object;

import java.io.Serializable;

import android.content.Context;
import android.preference.PreferenceManager;

/**
 * Sensor object definition
 * 
 * @author jean
 * 
 */
public class Sensor implements Serializable {
	private static final long serialVersionUID = 1L;
	public static final String SENSOR_TYPE_A1 = "A1";
    public static final String SENSOR_TYPE_A2 = "A2";
    public static final String SENSOR_TYPE_TEMP1 = "Temp1";
    public static final String SENSOR_TYPE_TEMP2 = "Temp2";

	String mName = null;
    String mLabel = null;
	Boolean isActivated = false;
	float coef = 1;
	String unitee = null;
	int nbDecimal = 3;

	/**
	 * Constructor
	 * 
	 * @param Sensor
	 */
	public Sensor(String name, float coef, String unitee, int nbDec, String label,
			Boolean isAct) {
		this.mName = name;
		this.coef = coef;
		this.unitee = unitee;
		this.nbDecimal = nbDec;
		this.isActivated = isAct;
        this.mLabel = label;
	}

    public String getLabel() { return mLabel; }

	public String getName() {
		return mName;
	}
	
	public String getValue(int val) {
		return String.format("%." + nbDecimal + "f", val * coef);
	}

	/**
	 * load sensor object from shared preference
	 * 
	 * @param ctx
	 *            context
	 * @param sensorType
	 *            sensor type
	 * @return Sensor object
	 */
	public static Sensor loadSensor(Context ctx, String sensorType) {
        String name = "name" + sensorType;
        String label = PreferenceManager.getDefaultSharedPreferences(ctx)
                .getString("label" + sensorType, "");
		Boolean isShow = PreferenceManager.getDefaultSharedPreferences(ctx)
				.getBoolean("activated" + sensorType, false);
		float coef = Float.parseFloat(PreferenceManager
				.getDefaultSharedPreferences(ctx).getString(
						"coef" + sensorType, "1"));
		String unitee = PreferenceManager.getDefaultSharedPreferences(ctx)
				.getString("unity" + sensorType, "");
		int nbDec = Integer.parseInt(PreferenceManager
				.getDefaultSharedPreferences(ctx).getString("dec" + sensorType,
						"3"));
		return new Sensor(name, coef, unitee, nbDec, label, isShow);
	}
}
